﻿@using NewLife;
@using NewLife.Cube
@using XCode;
@using XCode.Configuration;
@{
    var pair = (ValueTuple<IEntity, FieldItem>)Model;
    var entity = pair.Item1;
    var item = pair.Item2;

    var fields = ViewBag.Fields as FieldCollection;
    var dataField = fields.GetField(item.Name);
    var dataSource = dataField?.DataSource;

    var err = 0;
    if (ViewData.ModelState.TryGetValue(item.Name, out var st) && st != null) { err = st.Errors.Count; }

    var des = item.Description.TrimStart(item.DisplayName).TrimStart(",", ".", "，", "。");
    var display = dataField?.DisplayName ?? item.DisplayName;

    var total = 12;
    var label = 3;
    var span = 4;
    if (err == 0 && des.IsNullOrEmpty())
    {
        span = 0;
    }
    else if (item.Type == typeof(Boolean) || item.Type.IsEnum)
    {
        span += 1;
    }
    var input = total - label - span;
}
<label class="control-label col-xs-@(label) col-sm-@label">@display</label>
<div class="input-group col-xs-@(total - label) col-sm-@input">
    @if (dataSource != null)
    {
        @Html.ForListBox(item.Name, dataSource(entity, item), entity[item.Name] as String)
    }
    else
    {
        @Html.ForEditor(item, entity)
    }
</div>
@if (err > 0)
{
    <span class="col-sm-@span alert alert-danger">
        @Html.ValidationMessage(item.Name)
    </span>
}
else if (!des.IsNullOrEmpty())
{
    <span class="hidden-xs col-sm-@span">@Html.ForDescription(item)</span>
}